package king.dominic.jlibrary.util;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/8/16.
 */

public class ContactsUtils {
    private ContactsUtils() {
    }

    @NonNull
    public static ArrayList<People> query(Context context) {
        String[] cols = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
        Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                cols, null, null, null);
        ArrayList<People> array = new ArrayList<>();
        if (cursor == null) return array;
        while (cursor.moveToNext()) {
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            array.add(new People(name, number));
        }
        cursor.close();
        return array;
    }

    public static class People {
        private String name;
        private String number;

        private People(String name, String number) {
            this.name = name;
            this.number = number;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }
    }
}
